package com.aabte.nettyleran.util;

import io.netty.handler.codec.marshalling.*;
import org.jboss.marshalling.*;
import org.jboss.marshalling.serial.SerialMarshallerFactory;

import java.io.IOException;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/3/15
 */
public class MarshallingCodecFactory {

    public static MarshallingEncoder buildMarshallingEncoder() {
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration);
        return new MarshallingEncoder(provider);
    }

    public static MarshallingDecoder buildMarshallingDecoder() {
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);
        return new MarshallingDecoder(provider, 1024);
    }

    public static Unmarshaller buildUnMarshalling() throws IOException {
        MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        SerialMarshallerFactory factory = new SerialMarshallerFactory();
        return factory.createUnmarshaller(configuration);
    }

    public static Marshaller buildMarshalling() throws IOException {
        MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        SerialMarshallerFactory factory = new SerialMarshallerFactory();
        return factory.createMarshaller(configuration);
    }
}
